package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.dao.ClassCourseDao;
import com.cyzy.dao.StudentCourseDao;
import com.cyzy.dto.Result;
import com.cyzy.mapper.ClassMapper;
import com.cyzy.mapper.StudentCourseMapper;
import com.cyzy.pojo.Class;
import com.cyzy.pojo.Course;
import com.cyzy.pojo.Student;
import com.cyzy.pojo.StudentCourse;
import com.cyzy.service.ClassService;
import com.cyzy.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private StudentCourseMapper studentCourseMapper;

    @Autowired
    private StudentCourseService studentCourseService;


    @Override
    public Page<ClassCourseDao> selectClass(ClassCourseDao classCourseDao) {
        Page<ClassCourseDao> page = new Page<>(classCourseDao.getPageNum(), classCourseDao.getPageSize());
        return classMapper.selectAllClass(page,classCourseDao.getCourseId(),classCourseDao.getStudentName());
    }

    @Override
    public List<ClassCourseDao> selectClassBycourseName(ClassCourseDao classCourseDao) {
        List<ClassCourseDao> classCourseDaoList = classMapper.selectClassBycourseName(classCourseDao);
        return classCourseDaoList;
    }

    @Override
    public List<Course> selectAllCourse(Course course) {
        return classMapper.selectAllCourse(course);
    }

    @Override
    public int addClass(Class classs) {

        return  classMapper.insert(classs);

    }

    @Override
    public List<Class> getAllClass(Class classs) {
        return classMapper.getAllClassBycampusId(classs);
    }

    @Override
    public Result getClassStudent(int classId, int courseId) {

        List<ClassCourseDao> studentCourses = classMapper.selectAllClassStudent(classId, courseId);


        LambdaQueryWrapper<StudentCourse> classWrapper2 = new LambdaQueryWrapper<>();
        classWrapper2.eq(StudentCourse::getClassId, classId);
        List<StudentCourse> studentCourses1 = studentCourseMapper.selectList(classWrapper2);

        return Result.ok().data("allClassStudent", studentCourses).data("classStudent", studentCourses1);
    }

    @Override
    public Result classManagement(ClassCourseDao classCourseDao) {
        System.out.println(classCourseDao + "-----------");
        LambdaUpdateWrapper<StudentCourse> updateWrapper = new LambdaUpdateWrapper<>();
        List<Integer> studentIds = classCourseDao.getStudentIds();

        updateWrapper.set(StudentCourse::getClassId, classCourseDao.getClassId())
                .eq(StudentCourse::getCourseId, classCourseDao.getCourseId());
        for (Integer studentId : studentIds) {
            updateWrapper.eq(StudentCourse::getStudentId, studentId).or();
        }
        LambdaUpdateWrapper<StudentCourse> queryWrapper =
                new LambdaUpdateWrapper<StudentCourse>().set(StudentCourse::getClassId, 0)
                        .eq(StudentCourse::getClassId, classCourseDao.getClassId());
        studentCourseService.update(queryWrapper);
        studentCourseService.update(updateWrapper);
        return Result.ok().message("分班成功");
    }

    @Override
    public int updateClass(Class classs) {

        return classMapper.updateById(classs);
    }

    @Override
    public Class showClass(Class classs) {
        classMapper.selectById(classs.getClassId());
        return classMapper.selectById(classs.getClassId());
    }
}
